
<template>
  <div>
    <SearchForm
        @search="onSearch"
        @reset="onReset"
        :search-list="searchList" :search-params="searchParams">
    </SearchForm>

    <div class="table-layout">
      <el-card shadow="never">
        <div class="table-handle">
          <el-button type="primary" size="mini" icon="el-icon-plus" @click="onAdd">新增</el-button>
        </div>
        <div class="table-box">
          <el-table
              size="large"
              stripe
              border
              v-loading="loading"
              max-height="500px"
              :data="tableData"
              row-key="id"
              element-loading-spinner="el-icon-loading"
              element-loading-background="rgba(255, 255, 255, 0.8)"
              @selection-change="onSelect"
          >
            <el-table-column align="center" label="课程名称" prop="name" width="120px" />
            <el-table-column align="center" label="课程图片" prop="logo" width="100px">
              <template #default="scope">
                <el-image v-if="scope.row.logo" :src="scope.row.logo" style="width: 60px; height: 60px" />
                <span v-else>-</span>
              </template>
            </el-table-column>
            <el-table-column align="center" label="上课地点" prop="location" width="120px" />
            <el-table-column align="center" label="总人数" prop="total_count" width="120px" />
            <el-table-column align="center" label="已报名人数" prop="signed_count" width="120px" />
            <el-table-column align="center" label="上课时间" prop="schedule" width="200px" />
            <el-table-column align="center" label="上课年级" prop="grade" width="120px" >
              <template #default="scope">
                <template v-if="scope.row.grade">
                  {{ formatGarde(scope.row.grade) }}
                </template>
                <span v-else>-</span>
              </template>
            </el-table-column>
            <el-table-column align="center" label="教师姓名" prop="teacher_name" width="100px" />
            <el-table-column align="center" label="状态" prop="status" width="80px">
              <template #default="scope">
                <el-tag type="success" v-if="scope.row.status === 1">正常</el-tag>
                <el-tag type="info" v-else>禁用</el-tag>
              </template>
            </el-table-column>
            <el-table-column align="center" label="创建时间" prop="create_time" width="160px" />
            <el-table-column label="操作" fixed="right" width="150px">
              <template slot-scope="scope">
                <el-button type="text" @click="onRecord(scope.row)">报名记录</el-button>
                <el-button type="text" @click="onEdit(scope.row)">编辑</el-button>
                <el-button type="text" style="color: #F82222" @click="onDelete(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-card>

      <div class="page-box">
        <el-pagination
            background
            :current-page="pageNo"
            :page-sizes="pageSizes"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalSize"
            @size-change="changePageSize"
            @current-change="changePageNo"
        />
      </div>
    </div>
    <Add ref="addFormRef" @get-page="initData" />
  </div>
</template>
<script>

import crud from "@/mixins/crud";
import { http } from "@/api";
import Add from "./add";
export default {
  name: "index",
  components: {
    Add
  },
  mixins: [crud],
  data() {
    return {
      request: http.course,
      searchList: [
        {
          label: '课程名称',
          prop: 'name',
          name: 'input',
          clearable: true
        },
        {
          label: '上课地点',
          prop: 'location',
          name: 'input',
          clearable: true
        },
        {
          label: '教师姓名',
          prop: 'teacher_name',
          name: 'input',
          clearable: true
        },
        {
          label: '状态',
          prop: 'status',
          name: 'select',
          clearable: true,
          options: [
            { label: '正常', value: 1 },
            { label: '禁用', value: 0 }
          ]
        }
      ],
      searchParams: {
        name: '',
        location: '',
        teacher_name: '',
        status: ''
      },
      tableData: []
    }
  },
  methods: {
    onRecord(row) {
      this.$router.push({
        path: '/sign',
        query: {
          course_id: row.id
        }
      })
    },
    formatGarde(grade) {
      if (!grade) return '-'
      const grades = {
        '1': '一年级',
        '2': '二年级',
        '3': '三年级',
        '4': '四年级',
        '5': '五年级',
        '6': '六年级',
      }
      try {
        const gardeArr = grade.split(',')
        return gardeArr.map(garde => grades[garde]).join('、')
      } catch (e) {
        return grade
      }
    }
  }
}
</script>

<style scoped lang="scss">
</style>
